$(function(){
  new Vue({
    el: '#app',
    data() {
      return {
        isShow: false,
        formValidate: {
          payee_account: '',
          pay_pwd: '',
          money: null,
          fee: 0.0015,
          interval: 5,
        },
        ruleValidate: {
          payee_account: [
            { required: true, message: '收款账户不能为空' }
          ],
          pay_pwd: [
            { required: true, message: '支付密码不能为空' }
          ],
          money: [
            { required: true, message: '最低转账金额不能为空' }
          ]
        }
      }
    },
    methods: {
      handleSubmit() {
        this.$refs.formValidate.validate((valid) => {
          if (valid) {
            this.$Message.info('保存成功')
            localStorage.transfer_info = JSON.stringify(this.formValidate)
            chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
              chrome.tabs.sendMessage(tabs[0].id, {
                type: 'info',
                data: this.formValidate
              })
            })
          }
        })
      },
      handleClear() {
        this.$Message.info('清除成功')
        localStorage.removeItem('transfer_info');
        this.formValidate = {
          payee_account: '',
          pay_pwd: '',
          money: null,
          fee: 0.0015,
          interval: 5,
        }
        chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
          chrome.tabs.sendMessage(tabs[0].id, {
            type: 'clear'
          })
        })
      }
    },
    created() {
      if (localStorage.transfer_info) {
        let transfer_info = JSON.parse(localStorage.transfer_info);
        Object.assign(this.formValidate, transfer_info)
        chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
          chrome.tabs.sendMessage(tabs[0].id, {
            type: 'info',
            data: this.formValidate
          })
        })
      }
    }
  })
})
